
package com.gemframework.modules.prekit.flow.controller;

import com.gemframework.model.common.BaseResultData;
import com.gemframework.model.common.PageInfo;
import com.gemframework.modules.prekit.flow.entity.GemDeployment;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: DeploymentController
 * @Package: com.gemframework.modules.perkit.demo
 * @Date: 2020-04-16 13:36:13
 * @Version: v1.0
 * @Description: 发布流程管理
 * @Author: nine QQ 769990999
 * @Copyright: Copyright (c) 2020 wanyong
 * @Company: www.gemframework.com
 */
@RestController
@RequestMapping("deployments")
public class DeploymentController{

    @Autowired
    RepositoryService repositoryService;


    @GetMapping("/page")
    public BaseResultData page(PageInfo pageInfo,String name) {
        List<Deployment> deployments;
        long count;
        if(StringUtils.isNotBlank(name)) {
            deployments = repositoryService.createDeploymentQuery()
                    .deploymentName(name).orderByDeploymenTime().desc()
                    .listPage(pageInfo.getLimit().intValue() * (pageInfo.getPage().intValue() - 1), pageInfo.getLimit().intValue());
            count = repositoryService.createDeploymentQuery().deploymentName(name).count();
        }else{
            deployments = repositoryService.createDeploymentQuery()
                    .orderByDeploymenTime().desc()
                    .listPage(pageInfo.getLimit().intValue() * (pageInfo.getPage().intValue() - 1), pageInfo.getLimit().intValue());
            count = repositoryService.createDeploymentQuery().count();
        }
        List<GemDeployment> list = new ArrayList<>();
        for(Deployment deployment: deployments){
            list.add(new GemDeployment(deployment));
        }
        return BaseResultData.SUCCESS(list,count);
    }


    @GetMapping("/info")
    public BaseResultData info(String id) {
        Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(id).singleResult();
        return BaseResultData.SUCCESS(deployment);
    }

    @GetMapping("/delete")
    public BaseResultData delete(String id) {
        repositoryService.deleteDeployment(id);
        return BaseResultData.SUCCESS();
    }
}
